Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
how to pick some value from an object {} and put it into an array []

got some information from an API and now want to pick "correct_answer" property from all of them and then make an array from correct_answer here is the API information :

apipresave=[
  {question: "What does the "MP" stand for in MP3?",
   correct_answer: "Moving Picture",
   incorrect_answers: ["Music Player", "Multi Pass", "Micro Point"]},

  {question: "What does GHz stand for?", 
   correct_answer: "Gigahertz", 
   incorrect_answers: ["Gigahotz", "Gigahetz", "Gigahatz"]}, 

  {question: "In the programming language Java, which of these keywords would you put on a variable 
   to make sure it doesn't get modified?", 
   correct_answer: "Final", 
   incorrect_answers: ["Static", "Private", "Public"]},
]

btw I have already put this information on a const and have full access to them in code I have tried to loop over them and use push to get them but it didn't work I put it below :

   //puting all correct_answer  into an array 
   function correct_answer_array_func (){
         const correct_answer_array =[]
         for(let i =0 ; i<4;i++){
             correct_answer_array.push(apipresave[i].correct_answer) 
                 return correct_answer_array
         }    
    }

so how to obtain "correct_answer" from all the objects and put them in an array
the final result will be :

 correct_answer_array  =["Moving Picture","Gigahertz","Final"]  // correct_answer values
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You're looking for the .map() Javascript Array method

Here is an example that should give you the array of values you are looking for.

const correct_answer_array = apipresave.map(item => item.correct_answer)
// ["Moving Picture","Gigahertz","Final"] 
about 4 years ago · Juan Pablo Isaza Denunciar

0

To answer the question in terms of the code you provide:

In the function correct_answer_array_func you immediately return after pushing one correct_answer to the array. Also, you hardcoded the length of the array, which I believe is not the best thing to do (is the API response guaranteed to be 4 objects?)

So, to make the function work, you need something similar to:

   //puting all correct_answer  into an array 
   function correct_answer_array_func (){
         const correct_answer_array =[]
         for(let i =0 ; i<apipresave.length;i++){
             correct_answer_array.push(apipresave[i].correct_answer) 
         }    
         return correct_answer_array
     
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda